package svc

import (
	"fmt"
	"github.com/shugen/models"
)

//Insert
func (s *Svc) InsertMenGong(mg *models.MenGong) error {

	if _, err := s.Engine.Insert(mg); err != nil {
		fmt.Println("新增失败:", mg, err)
		return err
	}
	return nil
}

//Update
func (s *Svc) UpdateMenGong(mg *models.MenGong) (*models.MenGong, error) {

	mengong := &models.MenGong{}
	//title和type不需要更新
	affect, err := s.Engine.ID(string(mg.MenGongId)).Omit("type").Omit("title").Update(mg)
	if err != nil {
		return nil, err
	}
	if affect > 0 {
		s.Engine.ID(string(mg.MenGongId)).Get(mengong)
	}
	return mengong, nil
}

//Select
func (s *Svc) SelectMenGong(mg *models.MenGong) (*models.MenGong, error) {

	mengong := &models.MenGong{}

	if _, err := s.Engine.Where("type = ?", mg.Type).Get(mengong); err != nil {
		return nil, err
	}
	return mengong, nil
}

//Select List
func (s *Svc) ListMenGong() ([]*models.MenGong, error) {

	var list []*models.MenGong

	if err := s.Engine.Find(&list); err != nil {
		return nil, err
	}
	return list, nil
}
